package com.xiaoge.libraryuse.user_define_refresh_header;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.wang.avi.AVLoadingIndicatorView;
import com.xiaoge.libraryuse.R;

/**
 * Created by guoyzh on 2018/3/21 0021.
 */

public class UserDefineRefreshHeader extends LinearLayout implements RefreshHeader {

    private TextView mHeaderText;//标题文本
    private ImageView mArrowImage;//下拉箭头图标
    private AVLoadingIndicatorView mLoadingView;// 刷新控件

    public UserDefineRefreshHeader(Context context) {
        super(context);
        initView(context);
    }

    public UserDefineRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public UserDefineRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    private void initView(Context context) {
        setGravity(Gravity.CENTER);
        mHeaderText = new TextView(context);
        mHeaderText.setTextSize(12);
        mHeaderText.setTextColor(getResources().getColor(R.color.text_gray));

        // 设置自定义图片的下拉箭头
        mArrowImage = new ImageView(context);
        mArrowImage.setImageResource(R.mipmap.main_pulldown);

        mLoadingView = new AVLoadingIndicatorView(context);
        mLoadingView.setIndicatorColor(Color.parseColor("#00ffff"));
        addView(mLoadingView, DensityUtil.dp2px(26), DensityUtil.dp2px(26));

        // 添加自定义下拉箭头
        addView(mArrowImage, DensityUtil.dp2px(15), DensityUtil.dp2px(15));

        addView(new View(context), DensityUtil.dp2px(5), DensityUtil.dp2px(5));
        addView(mHeaderText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        setMinimumHeight(DensityUtil.dp2px(50));
    }

    @NonNull
    @Override
    public View getView() {
        return this;//真实的视图就是自己，不能返回null
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;//指定为平移，不能null
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int extendHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {
        mLoadingView.show();//开始动画
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        mLoadingView.hide();//停止动画
        if (success) {
            mHeaderText.setText("刷新完成");
        } else {
            mHeaderText.setText("刷新失败");
        }
        return 500;//延迟500毫秒之后再弹回
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case None:
            case PullDownToRefresh:
                mHeaderText.setText("下拉刷新");
                mLoadingView.setVisibility(GONE);//隐藏动画
                mArrowImage.setVisibility(VISIBLE);//显示下拉箭头
                mArrowImage.animate().rotation(0);//还原箭头方向
                break;
            case Refreshing:
                mHeaderText.setText("正在同步数据");
                mLoadingView.setVisibility(VISIBLE);//显示加载动画
                mArrowImage.setVisibility(GONE);//隐藏箭头
                break;
            case ReleaseToRefresh:
                mHeaderText.setText("松开刷新数据");
                mArrowImage.animate().rotation(180);//显示箭头改为朝上
                break;
        }
    }
}
